﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

using System.Threading;
using Microsoft.Phone.Info;

namespace TCP_Socket
{
    public partial class MainPage : PhoneApplicationPage
    {
        private int _ECHO_PORT;
        SocketClient _client;
        Akcelerometr _akc;
        Thread _thread;


        public MainPage()
        {
            InitializeComponent();
            txbID.Text = GetDeviceUniqueID();
            _ECHO_PORT = Convert.ToInt32(txbPort.Text);
        }

        public string GetDeviceUniqueID()
        {
            byte[] result = null;
            object uniqueId;
            string val;
            if (DeviceExtendedProperties.TryGetValue("DeviceUniqueId", out uniqueId))
            {
                result = (byte[])uniqueId;
            }
            return val = Convert.ToBase64String(result);
        } 

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            ClearLog();

            _client = new SocketClient();

            Log(String.Format("Connecting to server '{0}' over port {1} (echo) ...", txtRemoteHost.Text, _ECHO_PORT), true);
            string result = _client.Connect(txtRemoteHost.Text, _ECHO_PORT);
            Log(result, false);

            //_client.Send("Client ID: " + GetDeviceUniqueID());
 
            Log("Requesting Receive ...", true);
            _thread = new Thread(new ThreadStart(_client.Receive));
            _thread.Start();
           
            //Log("Requesting Receive ...", true);
            //_client.Receive();
            //result = _client.Message;

            while (_client.Message == null){}
            result = _client.Message;

            Log("Receive: " + result, false);

            try
            {
                switch (Convert.ToInt32(result))
                {
                    case 666:
                        _akc = new Akcelerometr(this, _client);
                        _akc.AkcStart();
                        break;

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("switch error: " + ex.Message);
            }
        }


        public void Log(string message, bool isOutgoing)
        {
            this.Dispatcher.BeginInvoke(delegate()
            {
                //mainPage.Log(e.X.ToString() + e.Y.ToString() + e.Z.ToString(), true);
                string direction = (isOutgoing) ? ">> " : "<< ";
                txtOutput.Text += Environment.NewLine + direction + message;
            });
        }


        private void ClearLog()
        {
            txtOutput.Text = String.Empty;
        }

        private void txtRemoteHost_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void btnAkcStart_Click(object sender, RoutedEventArgs e)
        {
            _akc = new Akcelerometr(this, _client);
            _akc.AkcStart();
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                _client.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Client stop error: " + ex.Message);
            }

          
        }

        private void btnAkcStop_Click(object sender, RoutedEventArgs e)
        {
            _akc.AkcStop();
           
        }

    }
}